package sg.edu.nus.iss.billing.dao.bill;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.BadDbCallException;
import sg.edu.nus.iss.billing.vo.bill.PaymentVO;

public class PaymentHistoryDAO implements IPaymentHistoryDAO {
	
	private ArrayList<String> data = null;
	private final static String FILE_PATH = "data//paymentHistory.txt";
	
	public PaymentHistoryDAO(){
		try {
			data = (ArrayList<String>) DbUtil.loadDb(FILE_PATH);
		} catch (BadDbCallException e) {
			data = new ArrayList<String>();
			System.out.println(e.getMessage());
		}
	}
	
	public void printDb(){
		for (int i=0;i<data.size();i++ ){
			System.out.println(data.get(i));
		}
	}
	
	@Override
	public List<PaymentVO> getPaymentMade(String accountNo, Date start, Date end) {
		ArrayList<PaymentVO> paymentHistory = new ArrayList<PaymentVO>();
		for(int i=0;i<data.size();i++){
			String[] tokens =  (data.get(i)).split(java.util.regex.Pattern.quote("^"));
			if (tokens.length ==3 && tokens[0].equals(accountNo)) {
				PaymentVO payment = new PaymentVO(tokens[0], tokens[1], tokens[2]);
				Date paymentDate = Util.getDateFromString(tokens[2]);
				if (Util.isBetweenDate(start, end, paymentDate)) {
					paymentHistory.add(payment);	
				}
			}
		}
		return paymentHistory;
	}
}
